 package server.squad;
 import client.MapleCharacter;
import constants.JobConstants;
import handling.channel.ChannelServer;
import handling.world.WorldFindService;
import server.Timer;
import server.maps.MapleMap;
import tools.MaplePacketCreator;
import tools.Pair;

import java.lang.ref.WeakReference;
import java.util.*;
import java.util.concurrent.ScheduledFuture;
















 public class MapleSquad {
   private final String leaderName;
   private final String toSay;
/*  21 */   private final Map<String, String> members = new LinkedHashMap<>();
/*  22 */   private final Map<String, String> bannedMembers = new LinkedHashMap<>();
   private final int ch;
   private final long startTime;
   private final int expiration;
   private final int beginMapId;
   private final MapleSquadType type;
   private WeakReference<MapleCharacter> leader;
/*  29 */   private byte status = 0;




   private ScheduledFuture<?> removal;





   public MapleSquad(int ch, String type, MapleCharacter leader, int expiration, String toSay) {
/*  41 */     this.leader = new WeakReference<>(leader);
/*  42 */     this.members.put(leader.getName(), JobConstants.getJobBasicNameById(leader.getJob()));
/*  43 */     this.leaderName = leader.getName();
/*  44 */     this.ch = ch;
/*  45 */     this.toSay = toSay;
/*  46 */     this.type = MapleSquadType.valueOf(type.toLowerCase());
/*  47 */     this.status = 1;
/*  48 */     this.beginMapId = leader.getMapId();
/*  49 */     leader.getMap().setSquad(this.type);
/*  50 */     if (this.type.queue.get(Integer.valueOf(ch)) == null) {
/*  51 */       this.type.queue.put(Integer.valueOf(ch), new ArrayList<>());
/*  52 */       this.type.queuedPlayers.put(Integer.valueOf(ch), new ArrayList<>());
     }
/*  54 */     this.startTime = System.currentTimeMillis();
/*  55 */     this.expiration = expiration;
   }

   public void copy() {
/*  59 */     while (((ArrayList)this.type.queue.get(Integer.valueOf(this.ch))).size() > 0 && ChannelServer.getInstance(this.ch).getMapleSquad(this.type) == null) {
/*  60 */       int index = 0;
/*  61 */       long lowest = 0L;
/*  62 */       for (int i = 0; i < ((ArrayList)this.type.queue.get(Integer.valueOf(this.ch))).size(); i++) {
/*  63 */         if (lowest == 0L || ((Long)((Pair)((ArrayList)this.type.queue.get(Integer.valueOf(this.ch))).get(i)).right).longValue() < lowest) {
/*  64 */           index = i;
/*  65 */           lowest = ((Long)((Pair)((ArrayList)this.type.queue.get(Integer.valueOf(this.ch))).get(i)).right).longValue();
         }
       }
/*  68 */       String nextPlayerId = (String)((Pair)((ArrayList)this.type.queue.get(Integer.valueOf(this.ch))).remove(index)).left;
/*  69 */       int theirCh = WorldFindService.getInstance().findChannel(nextPlayerId);
/*  70 */       if (theirCh > 0) {
/*  71 */         MapleCharacter lead = ChannelServer.getInstance(theirCh).getPlayerStorage().getCharacterByName(nextPlayerId);
/*  72 */         if (lead != null && lead.getMapId() == this.beginMapId && lead.getClient().getChannel() == this.ch) {
/*  73 */           MapleSquad squad = new MapleSquad(this.ch, this.type.name(), lead, this.expiration, this.toSay);
/*  74 */           if (ChannelServer.getInstance(this.ch).addMapleSquad(squad, this.type.name())) {
/*  75 */             getBeginMap().broadcastMessage(MaplePacketCreator.getClock(this.expiration / 1000));
/*  76 */             getBeginMap().broadcastMessage(MaplePacketCreator.serverNotice(6, nextPlayerId + this.toSay));
/*  77 */             ((ArrayList)this.type.queuedPlayers.get(this.ch)).add(new Pair(nextPlayerId, "Success"));

                   }else {
                    squad.clear();
                    ((ArrayList)this.type.queuedPlayers.get(this.ch)).add(new Pair(nextPlayerId, "Skipped"));
                }

                         }
/*  84 */         if (lead != null) {
/*  85 */           lead.dropMessage(6, "Your squad has been skipped due to you not being in the right channel and map.");
         }
/*  87 */
       }
/*  91 */       this.getBeginMap().broadcastMessage(MaplePacketCreator.serverNotice(6, nextPlayerId + "'s squad has been skipped due to the player not being in the right channel and map."));
        ((ArrayList)this.type.queuedPlayers.get(this.ch)).add(new Pair(nextPlayerId, "Not in map"));

             }
   }


   public MapleMap getBeginMap() {
/*  98 */     return ChannelServer.getInstance(this.ch).getMapFactory().getMap(this.beginMapId);
   }

   public void clear() {
/* 102 */     if (this.removal != null) {
/* 103 */       getBeginMap().broadcastMessage(MaplePacketCreator.stopClock());
/* 104 */       this.removal.cancel(false);
/* 105 */       this.removal = null;
     }
/* 107 */     this.members.clear();
/* 108 */     this.bannedMembers.clear();
/* 109 */     this.leader = null;
/* 110 */     ChannelServer.getInstance(this.ch).removeMapleSquad(this.type);
/* 111 */     this.status = 0;
   }

   public MapleCharacter getChar(String name) {
/* 115 */     return ChannelServer.getInstance(this.ch).getPlayerStorage().getCharacterByName(name);
   }

   public long getTimeLeft() {
/* 119 */     return this.expiration - System.currentTimeMillis() - this.startTime;
   }

   public void scheduleRemoval() {
/* 123 */     this.removal = Timer.EtcTimer.getInstance().schedule(() -> { if (this.status != 0 && this.leader != null && (getLeader() == null || this.status == 1)) { clear(); copy(); }  },this.expiration);
   }






   public String getLeaderName() {
/* 132 */     return this.leaderName;
   }




   public List<Pair<String, Long>> getAllNextPlayer() {
/* 139 */     return this.type.queue.get(Integer.valueOf(this.ch));
   }

   public boolean containsNextPlayer(MapleCharacter player) {
/* 143 */     for (Pair<String, Long> names : this.type.queue.get(Integer.valueOf(this.ch))) {
/* 144 */       if (((String)names.left).equalsIgnoreCase(player.getName())) {
/* 145 */         return true;
       }
     }
/* 148 */     return false;
   }

   public String getNextPlayer() {
/* 152 */     StringBuilder sb = new StringBuilder("\n远征人数 : ");
/* 153 */     sb.append("#b").append(((ArrayList)this.type.queue.get(Integer.valueOf(this.ch))).size()).append(" #k ").append("远征队成员信息 : \n\r ");
/* 154 */     int i = 0;
/* 155 */     for (Pair<String, Long> chr : this.type.queue.get(Integer.valueOf(this.ch))) {
/* 156 */       i++;
/* 157 */       sb.append(i).append(" : ").append((String)chr.left);
/* 158 */       sb.append(" \n\r ");
     }
/* 160 */     sb.append("Would you like to #ebe next#n in the queue, or #ebe removed#n from the queue if you are in it?");
/* 161 */     return sb.toString();
   }

   public void setNextPlayer(String chr) {
/* 165 */     Pair<String, Long> toRemove = null;
/* 166 */     for (Pair<String, Long> s : this.type.queue.get(Integer.valueOf(this.ch))) {
/* 167 */       if (((String)s.left).equals(chr)) {
/* 168 */         toRemove = s;
         break;
       }
     }
/* 172 */     if (toRemove != null) {
/* 173 */       ((ArrayList)this.type.queue.get(Integer.valueOf(this.ch))).remove(toRemove);
       return;
     }
/* 176 */     for (ArrayList<Pair<String, Long>> v : this.type.queue.values()) {
/* 177 */       for (Pair<String, Long> s : v) {
/* 178 */         if (((String)s.left).equals(chr)) {
           return;
         }
       }
     }
/* 183 */     ((ArrayList)this.type.queue.get(this.ch)).add(new Pair(chr, System.currentTimeMillis()));
       }

   public MapleCharacter getLeader() {
/* 187 */     if (this.leader == null || this.leader.get() == null) {
/* 188 */       if (this.members.size() > 0 && getChar(this.leaderName) != null) {
/* 189 */         this.leader = new WeakReference<>(getChar(this.leaderName));
       } else {
/* 191 */         if (this.status != 0) {
/* 192 */           clear();
         }
/* 194 */         return null;
       }
     }
/* 197 */     return this.leader.get();
   }

   public boolean containsMember(MapleCharacter member) {
/* 201 */     for (String mmbr : this.members.keySet()) {
/* 202 */       if (mmbr.equalsIgnoreCase(member.getName())) {
/* 203 */         return true;
       }
     }
/* 206 */     return false;
   }

   public List<String> getMembers() {
/* 210 */     return new LinkedList<>(this.members.keySet());
   }

   public List<String> getBannedMembers() {
/* 214 */     return new LinkedList<>(this.bannedMembers.keySet());
   }

   public int getSquadSize() {
/* 218 */     return this.members.size();
   }

   public boolean isBanned(MapleCharacter member) {
/* 222 */     return this.bannedMembers.containsKey(member.getName());
   }

   public int addMember(MapleCharacter member, boolean join) {
/* 226 */     if (getLeader() == null) {
/* 227 */       return -1;
     }
/* 229 */     String job = JobConstants.getJobBasicNameById(member.getJob());
/* 230 */     if (join) {
/* 231 */       if (!containsMember(member) && !containsNextPlayer(member)) {
/* 232 */         if (this.members.size() <= 30) {
/* 233 */           this.members.put(member.getName(), job);
/* 234 */           getLeader().dropMessage(5, member.getName() + " (" + job + ") 加入了远征队.");
/* 235 */           return 1;
         }
/* 237 */         return 2;
       }
/* 239 */       return -1;
     }
/* 241 */     if (containsMember(member)) {
/* 242 */       this.members.remove(member.getName());
/* 243 */       getLeader().dropMessage(5, member.getName() + " (" + job + ") 离开了远征队.");
/* 244 */       return 1;
     }
/* 246 */     return -1;
   }


   public void acceptMember(int pos) {
/* 251 */     if (pos < 0 || pos >= this.bannedMembers.size()) {
       return;
     }
/* 254 */     List<String> membersAsList = getBannedMembers();
/* 255 */     String toadd = membersAsList.get(pos);
/* 256 */     if (toadd != null && getChar(toadd) != null) {
/* 257 */       this.members.put(toadd, this.bannedMembers.get(toadd));
/* 258 */       this.bannedMembers.remove(toadd);
/* 259 */       getChar(toadd).dropMessage(5, getLeaderName() + " 将你列为远征队队员.");
     }
   }

   public void reAddMember(MapleCharacter chr) {
/* 264 */     removeMember(chr);
/* 265 */     this.members.put(chr.getName(), JobConstants.getJobBasicNameById(chr.getJob()));
   }

   public void removeMember(MapleCharacter chr) {
/* 269 */     if (this.members.containsKey(chr.getName())) {
/* 270 */       this.members.remove(chr.getName());
     }
   }

   public void removeMember(String chr) {
/* 275 */     if (this.members.containsKey(chr)) {
/* 276 */       this.members.remove(chr);
     }
   }

   public void banMember(int pos) {
/* 281 */     if (pos <= 0 || pos >= this.members.size()) {
       return;
     }
/* 284 */     List<String> membersAsList = getMembers();
/* 285 */     String toban = membersAsList.get(pos);
/* 286 */     if (toban != null && getChar(toban) != null) {
/* 287 */       this.bannedMembers.put(toban, this.members.get(toban));
/* 288 */       this.members.remove(toban);
/* 289 */       getChar(toban).dropMessage(5, getLeaderName() + " 将你请出远征队，目前无法加入远征队.");
     }
   }

   public int getStatus() {
/* 294 */     return this.status;
   }

   public void setStatus(byte status) {
/* 298 */     this.status = status;
/* 299 */     if (status == 2 && this.removal != null) {
/* 300 */       this.removal.cancel(false);
/* 301 */       this.removal = null;
     }
   }

   public int getBannedMemberSize() {
/* 306 */     return this.bannedMembers.size(); } public String getSquadMemberString(byte type) { StringBuilder sb;
     int i;
     Map<String, Integer> jobs;
     int selection;
/* 310 */     switch (type) {
       case 0:
/* 312 */         sb = new StringBuilder("总共 : ");
/* 313 */         sb.append("#b").append(this.members.size()).append(" #k ").append("个远征队成员 : \n\r ");
/* 314 */         i = 0;
/* 315 */         for (Map.Entry<String, String> chr : this.members.entrySet()) {
/* 316 */           i++;
/* 317 */           sb.append(i).append(" : ").append(chr.getKey()).append(" (").append(chr.getValue()).append(") ");
/* 318 */           if (i == 1) {
/* 319 */             sb.append("(远征队队长)");
           }
/* 321 */           sb.append(" \n\r ");
         }
/* 323 */         while (i < 30) {
/* 324 */           i++;
/* 325 */           sb.append(i).append(" : ").append(" \n\r ");
         }
/* 327 */         return sb.toString();

       case 1:
/* 330 */         sb = new StringBuilder("总共 : ");
/* 331 */         sb.append("#b").append(this.members.size()).append(" #k ").append("个远征队成员 : \n\r ");
/* 332 */         i = 0; selection = 0;
/* 333 */         for (Map.Entry<String, String> chr : this.members.entrySet()) {
/* 334 */           i++;
/* 335 */           sb.append("#b#L").append(selection).append("#");
/* 336 */           selection++;
/* 337 */           sb.append(i).append(" : ").append(chr.getKey()).append(" (").append(chr.getValue()).append(") ");
/* 338 */           if (i == 1) {
/* 339 */             sb.append("(远征队队长)");
           }
/* 341 */           sb.append("#l").append(" \n\r ");
         }
/* 343 */         while (i < 30) {
/* 344 */           i++;
/* 345 */           sb.append(i).append(" : ").append(" \n\r ");
         }
/* 347 */         return sb.toString();

       case 2:
/* 350 */         sb = new StringBuilder("总共 : ");
/* 351 */         sb.append("#b").append(this.members.size()).append(" #k ").append("个远征队成员 : \n\r ");
/* 352 */         i = 0; selection = 0;
/* 353 */         for (Map.Entry<String, String> chr : this.bannedMembers.entrySet()) {
/* 354 */           i++;
/* 355 */           sb.append("#b#L").append(selection).append("#");
/* 356 */           selection++;
/* 357 */           sb.append(i).append(" : ").append(chr.getKey()).append(" (").append(chr.getValue()).append(") ");
/* 358 */           sb.append("#l").append(" \n\r ");
         }
/* 360 */         while (i < 30) {
/* 361 */           i++;
/* 362 */           sb.append(i).append(" : ").append(" \n\r ");
         }
/* 364 */         return sb.toString();

       case 3:
/* 367 */         sb = new StringBuilder("Jobs : ");
/* 368 */         jobs = getJobs();
/* 369 */         for (Map.Entry<String, Integer> chr : jobs.entrySet()) {
/* 370 */           sb.append("\r\n").append(chr.getKey()).append(" : ").append(chr.getValue());
         }
/* 372 */         return sb.toString();
     }

/* 375 */     return null; }


   public MapleSquadType getType() {
/* 379 */     return this.type;
   }

   public Map<String, Integer> getJobs() {
/* 383 */     Map<String, Integer> jobs = new LinkedHashMap<>();
/* 384 */     for (Map.Entry<String, String> chr : this.members.entrySet()) {
/* 385 */       if (jobs.containsKey(chr.getValue())) {
/* 386 */         jobs.put(chr.getValue(), Integer.valueOf(((Integer)jobs.get(chr.getValue())).intValue() + 1)); continue;
       }
/* 388 */       jobs.put(chr.getValue(), Integer.valueOf(1));
     }

/* 391 */     return jobs;
   }
 }


/* Location:              C:\PGX\Server.jar!\server\squad\MapleSquad.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */